package net.ck.hzq;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import activity.glide.GlideApp;
import base.BaseAct;
import event.NetErrorEvent;
import util.LogUtils;
import util.StringUtils;

/**
 * 知识讲堂详情
 */
public class PicActivity extends BaseAct {

//    private ListView pic_lv;
    List<String> data;
    String bean;
    ListviewPicAdapter listviewPicAdapter;
    private ImageView webview;
    private String url, url1, index;
    private int index1;
    String name;

    @Override
    protected void setRoot() {
        setContentView(R.layout.activity_pic);
    }

    @Override
    protected void setup() {
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
//        pic_lv = findViewById(R.id.pic_lv);
        webview = (ImageView) findViewById(R.id.webview);
            url = getIntent().getStringExtra("url");
            if(url.contains("|")){
                url=url.replace("|","");
            }

            LogUtils.d(url);
            name = StringUtils.isEmpty(getIntent().getStringExtra("name")) ? "" : getIntent().getStringExtra("name");
            setToolBar(name, "");
//            index = url.substring(url.lastIndexOf("/") + 1, url.length());
//            url1 = url.substring(0, url.lastIndexOf("/") + 1);
//            index1 = Integer.parseInt(index);
//            data = new ArrayList<>();
//            data.add(url);


//
//            WebSettings webSettings = webview.getSettings();
//
//            webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
//            webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
//
//            webSettings.setSupportZoom(false); //支持缩放，默认为true。是下面那个的前提。
////        webSettings.setBuiltInZoomControls(true);
//            webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
//            webSettings.setDefaultTextEncodingName("utf-8");
//            webview.getSettings().setJavaScriptEnabled(true);
//            webview.getSettings().setDomStorageEnabled(true);
//
//            webview.loadUrl(url);

        GlideApp.with(PicActivity.this)
                .load(url)
                .into(webview);


            //初始化数据
//            for (int i = 1; i <= index1; i++) {
//                data.add(url1 + i + ".png");
//            }
//            listviewPicAdapter = new ListviewPicAdapter(data, PicActivity.this);
//            pic_lv.setAdapter(listviewPicAdapter);


    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    public class ListviewPicAdapter extends BaseAdapter {

        private List<String> data;//数据

        private Context context;//上下文

        public ListviewPicAdapter(List<String> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = LayoutInflater.from(context).inflate(R.layout.listviewpic_item, null, false);
                viewHolder = new ViewHolder();
                viewHolder.imageView = (ImageView) view.findViewById(R.id.lvpic_img);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            GlideApp.with(context)
                    .load(data.get(position))
                    .into(viewHolder.imageView);

            return view;
        }

        private class ViewHolder {
            ImageView imageView;
        }

    }


}
